package com.mlog.njyjy.ndop.share.zabbix.factory;

import com.mlog.njyjy.ndop.share.zabbix.client.DefaultZabbixClient;
import com.mlog.njyjy.ndop.share.zabbix.client.ZabbixClient;
import com.mlog.njyjy.ndop.share.zabbix.ex.ZabbixClientException;
import okhttp3.OkHttpClient;

import java.time.Duration;
import java.util.Objects;

/**
 * factory of {@link ZabbixClient}
 *
 * @author alex
 * @version v1.0 2020/6/29
 */
public class ZabbixClientFactory {

    private final ZabbixClientConfig clientConfig;

    public ZabbixClientFactory(ZabbixClientConfig clientConfig) {
        this.clientConfig = clientConfig;
    }


    /**
     * create client
     *
     * @return
     */
    public ZabbixClient getClient() {
        DefaultZabbixClient client = new DefaultZabbixClient();
        if (Objects.isNull(clientConfig)) {
            throw new ZabbixClientException("[zabbix client] Config cannot be null");
        }
        client.setServerUri(clientConfig.getApiUri());
        client.setShowRequestDetail(clientConfig.isShowRequestDetail());
        // TODO useinfo?

        OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                .connectTimeout(Duration.ofSeconds(clientConfig.getConnTimeout()))
                .readTimeout(Duration.ofSeconds(clientConfig.getReadTimeout()))
                .build();
        client.setHttpClient(okHttpClient);
        return client;
    }

}
